//
//  Sleep.c
//  ARDUINO_PROPELLER
//
//  Created by FRANCIS PAPINEAU on 12-02-06.
//  Copyright 2012 carleton university. All rights reserved.
//

//!SLEEP_MODE_IDLE         0
//!SLEEP_MODE_PWR_DOWN     1
//!SLEEP_MODE_PWR_SAVE     2
//!SLEEP_MODE_ADC          3
//!SLEEP_MODE_STANDBY      4
//!SLEEP_MODE_EXT_STANDBY  5

bool Sleep(void){
    
    checkVar = false;
    
    changeStateTo(6);
    //! attach interrupts to the Wakeup Pin, Serial Inputs,
    //! will wake up when a command is sent.
    
    set_sleep_mode(SleepState);   //! sleep mode is set here
    
    sleep_enable();          //! enables the sleep bit in the mcucr register
    //! so sleep is possible. just a safety pin 
    
    attachInterrupt(0, wakeUpNow, CHANGE); //! use interrupt 0 (pin 3) and run function
    //! wakeUpNow when pin 3 gets LOW // attached to the RX
    
    //! this int is attached to teh command RX/TX as well
    
    sleep_mode();            //! here the device is actually put to sleep!!
    //! THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
    
    sleep_disable();         //! first thing after waking from sleep:
    //! disable sleep...
    
    detachInterrupt(0);      //! disables interrupt 0 on pin 2 so the 
    //! wakeUpNow code will not be executed 
    //! during normal running time.
    
    
    if(checkVar){
        return true;
    }else{
        sendError(false, SleepAddress);
        return false;
    }
}